package com.fish.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fish.common.annotation.OperationLog;
import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.model.dto.DictDataDTO;
import com.fish.model.vo.DictDataVO;
import com.fish.service.DictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 字典数据控制器
 */
@Tag(name = "字典数据管理", description = "字典数据相关接口，包括字典数据增删改查等")
@RestController
@RequestMapping("/api/dict/data")
public class DictDataController {

    @Resource
    private DictDataService dictDataService;

    /**
     * 分页查询字典数据列表
     */
    @Operation(summary = "分页查询字典数据", description = "分页查询字典数据列表，支持字典类型和标签筛选")
    @Parameters({
        @Parameter(name = "pageNum", description = "页码", example = "1"),
        @Parameter(name = "pageSize", description = "每页大小", example = "10"),
        @Parameter(name = "dictType", description = "字典类型", required = false),
        @Parameter(name = "dictLabel", description = "字典标签", required = false)
    })
    @GetMapping("/list")
    @RequiresPermission("system:dict:list")
    public Result<Page<DictDataVO>> getPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String dictType,
            @RequestParam(required = false) String dictLabel) {
        Page<DictDataVO> page = dictDataService.getPage(pageNum, pageSize, dictType, dictLabel);
        return Result.success(page);
    }

    /**
     * 根据字典类型获取字典数据列表（用于前端渲染）
     */
    @Operation(summary = "根据类型查询字典数据", description = "根据字典类型获取字典数据列表，用于前端下拉框等渲染")
    @Parameter(name = "dictType", description = "字典类型", required = true, example = "sys_user_sex")
    @GetMapping("/type/{dictType}")
    public Result<List<DictDataVO>> getByType(@PathVariable String dictType) {
        List<DictDataVO> list = dictDataService.getByType(dictType);
        return Result.success(list);
    }

    /**
     * 根据ID获取字典数据详情
     */
    @Operation(summary = "根据ID查询字典数据", description = "根据字典数据ID查询详细信息")
    @Parameter(name = "id", description = "字典数据ID", required = true, example = "1")
    @GetMapping("/{id}")
    @RequiresPermission("system:dict:query")
    public Result<DictDataVO> getById(@PathVariable Long id) {
        DictDataVO vo = dictDataService.getById(id);
        return Result.success(vo);
    }

    /**
     * 添加字典数据
     */
    @Operation(summary = "添加字典数据", description = "创建新的字典数据项")
    @PostMapping
    @RequiresPermission("system:dict:add")
    @OperationLog("添加字典数据")
    public Result<Void> add(@Validated @RequestBody DictDataDTO dto) {
        dictDataService.add(dto);
        return Result.success();
    }

    /**
     * 更新字典数据
     */
    @Operation(summary = "更新字典数据", description = "更新字典数据信息")
    @PutMapping
    @RequiresPermission("system:dict:edit")
    @OperationLog("更新字典数据")
    public Result<Void> update(@Validated @RequestBody DictDataDTO dto) {
        dictDataService.update(dto);
        return Result.success();
    }

    /**
     * 删除字典数据
     */
    @Operation(summary = "删除字典数据", description = "根据ID删除字典数据")
    @Parameter(name = "id", description = "字典数据ID", required = true, example = "1")
    @DeleteMapping("/{id}")
    @RequiresPermission("system:dict:remove")
    @OperationLog("删除字典数据")
    public Result<Void> delete(@PathVariable Long id) {
        dictDataService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除字典数据
     */
    @Operation(summary = "批量删除字典数据", description = "批量删除多个字典数据")
    @DeleteMapping("/batch")
    @RequiresPermission("system:dict:remove")
    @OperationLog("批量删除字典数据")
    public Result<Void> deleteBatch(@RequestBody List<Long> ids) {
        dictDataService.deleteBatch(ids);
        return Result.success();
    }
}